package com.woniu.new_orange.finance_model_guo.controller;

import com.woniu.new_orange.entity.PushLevel;
import com.woniu.new_orange.finance_model_guo.Dto.PushLevelDto;
import com.woniu.new_orange.finance_model_guo.Vo.PushLevelVo;
import com.woniu.new_orange.finance_model_guo.service.PushLevelService;
import com.woniu.new_orange.util.ObjectExcelRead;
import com.woniu.new_orange.util.ObjectExcelView;
import com.woniu.new_orange.util.PageData;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: new_orange->PushLevelController
 * @Description:
 * @Author: 谭少琛的爸爸
 * @Date: 2023-02-02 15:07
 * @Version： 1.0
 **/
@RestController
@RequestMapping("push")
@Api(tags = "推送等级表")
public class PushLevelController {
    @Resource
    private PushLevelService pushLevelService;
    @ApiOperation(value = "条件查询",notes ="可以根据用户姓名，注册时间等查询" )
            @PostMapping("queryAll")
    public Result query(@RequestBody PushLevelDto pushLevelDto) {
                List<PushLevel> pushLevels = pushLevelService.selectAll(pushLevelDto );
                return Result.ok().data("List", pushLevels);
            }
    @ApiOperation(value = "修改",notes ="可以根据用户ID进行修改信息" )
     @GetMapping({"{id}"})
     public Result edit(@PathVariable long id){
         PushLevel pushLevel = pushLevelService.selectByPrimaryKey(id);
         return Result.ok().data("pushLevel",pushLevel);
     }
    @PutMapping("")
    public Result update(@RequestBody PushLevel pushLevel){
        int i = pushLevelService.updateByPrimaryKeySelective(pushLevel);
        return Result.ok();
    }
    @ApiOperation(value = "添加",notes ="添加用户" )
    @PostMapping("")
    public Result add(@Valid @RequestBody PushLevel pushLevel, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        int i = pushLevelService.insertSelective(pushLevel);
        return  Result.ok();
    }
    @ApiOperation(value = "删除",notes ="可以根据用户ID进行删除" )
    @DeleteMapping("{id}")
    public Result del(@PathVariable long id){
        int i = pushLevelService.deleteByPrimaryKey(id);
        return Result.ok();
    }
    @ApiOperation(value = "数据的导出",notes ="" )
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody PushLevelDto pushLevelDto) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("用户编号"); // 1
            titles.add("推送名称"); // 2
            titles.add("备注"); // 3
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<PushLevel> varOList = pushLevelService.selectExportData(pushLevelDto);
            List<PageData> varList = new ArrayList<>();
            for (PushLevel pushLevelVo : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1",pushLevelVo.getPushLevelId());
                vpd.put("var2",pushLevelVo.getPushLevelName());
                vpd.put("var3",pushLevelVo.getPushLevelDesc());

                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
    @ApiOperation(value = "数据的导入",notes ="" )
    @RequestMapping(value="/importExcel")
    public Result importExcel(MultipartFile file, HttpServletRequest request) throws Exception{

        if (null != file && !file.isEmpty()) {
            //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(file.getInputStream(), 1, 0, 0);
            for (PageData pageData : listPd) {
                PushLevel pushLevel = new PushLevel();
                String strId = pageData.getString("var0");
                int id=0;
                if(strId.indexOf(".")!=-1){
                    id = Integer.parseInt(strId.substring(0,strId.indexOf(".")));
                }else {
                    id = Integer.parseInt(strId);
                }
                pushLevel.setPushLevelId(id);
                pushLevel.setPushLevelName(pageData.getString("var1"));
                pushLevel.setPushLevelDesc(pageData.getString("var2"));
                pushLevelService.insertSelective(pushLevel);
            }

        }
        return Result.ok("导入成功");
    }
}
